﻿using ShooterGameMOD_Besiege.Player;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace ShooterGameMOD_Besiege.Weapon
{
    class BombScript:MonoBehaviour
    {
        //使用位置和方向
        public static Vector3 usingPosition = new Vector3(1.5f, 1f, 3f);
        public static Vector3 usingRotation = Vector3.right * -90f;
        //未使用位置和方向
        public static Vector3 nonusePosition = new Vector3(0f, 1f, -1f);
        public static Vector3 nonuseRotation = Vector3.right * 0f;

        public int bulletNumber = 1,
            bulletMaxNumber = 4,
            currentBulletNumber;
        public float fireRate = 24f;
        public float power = 12f;
        public WeaponScript.WeaponKind weaponKind = WeaponScript.WeaponKind.FourthWeapon;

        Rigidbody rigibody;
        ExplodeOnCollideBlock explodeOnCollideBlock;
        GameObject player;
        PlayerControllerScript playerControllerScript;
        PlayerWeaponScript playerWeapon;

        private void Start()
        {
            rigibody = GetComponent<Rigidbody>();
            explodeOnCollideBlock = GetComponent<ExplodeOnCollideBlock>();
            player = PlayerScript.player;
            playerWeapon = player.GetComponent<PlayerWeaponScript>();

            rigibody.isKinematic = true;
            transform.SetParent(player.transform);
            if (playerWeapon.currentWeaponKind == WeaponScript.WeaponKind.FourthWeapon)
            {
                setUseingPosition();
            }
            else
            {
                setUonusePosition();
            }

            playerControllerScript = player.GetComponent<PlayerControllerScript>();
            //crossBowBlock.Keys[0].AddOrReplaceKey(0, playerControllerScript.Fire);
            //playerControllerScript.Weapon = gameObject;
            //crossBowBlock.Sliders.Find(match => match.Key == "rate-of-fire").Value = fireRate;
            //crossBowBlock.Sliders.Find(match => match.Key == "power").Value = power;
            //currentBulletNumber = bulletNumber = Mathf.Min(crossBowBlock.ammo, bulletMaxNumber);
            //foreach (var ms in crossBowBlock.Sliders)
            //{
            //    Debug.Log(ms.Key);
            //}
        }

        private void Update()
        {
            if (StatMaster.isSimulating && playerWeapon.currentWeaponKind == WeaponScript.WeaponKind.FourthWeapon)
            {

                //if (playerControllerScript.key_DropWeapon.IsPressed && playerControllerScript.Weapon != null)
                //{
                //    Debug.Log("丢武器");

                //    rigibody.isKinematic = false;
                //    rigibody.AddRelativeForce(new Vector3(0, -750f, 500f));
                //    //crossBowBlock.Keys[0].AddOrReplaceKey(0, KeyCode.None);
                //    transform.SetParent(Machine.Active().SimulationMachine);
                //    Destroy(playerControllerScript.Weapon.GetComponent("CrossBowScript"));
                //    playerControllerScript.Weapon = null;
                //}
                if (playerWeapon.currentWeapon == gameObject && playerControllerScript.key_Fire.IsPressed)
                {
                    Debug.Log("扔炸弹");
                    transform.SetParent(Machine.Active().SimulationMachine);
                    rigibody.isKinematic = false;
                    rigibody.AddRelativeForce(new Vector3(0, -1000f, 800f));
                    playerWeapon.fourthWeapon = null;
                    playerWeapon.currentWeapon = null;
                    playerWeapon.currentWeaponKind = WeaponScript.WeaponKind.None;

                }


            }
        }

        public void setUseingPosition()
        {
            transform.position = player.transform.TransformPoint(usingPosition);
            transform.rotation = player.transform.rotation;
            transform.Rotate(usingRotation, Space.Self);
            transform.localScale = Vector3.one * 0.3f;
        }

        public void setUonusePosition()
        {
            transform.position = player.transform.TransformPoint(nonusePosition);
            transform.rotation = player.transform.rotation;
            transform.Rotate(nonuseRotation, Space.Self);
            transform.localScale = Vector3.one*0.3f;
        }

    }
}
